package com.yumeng.framework.persistence.mybatis.plugins.dynamictable;


import com.yumeng.common.exception.YumengException;
import com.yumeng.common.utils.SpringUtils;
import com.yumeng.config.helper.PropHelper;
import com.yumeng.config.properties.YumengMybatisPlusProperties;
import com.yumeng.framework.persistence.mybatis.plugins.dynamictable.parser.AbstractTableNameMapParser;
import com.yumeng.framework.persistence.mybatis.plugins.dynamictable.parser.DateTableNameParser;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * @author wxd
 * @date 2022/9/2 17:28
 */
@Slf4j
public class DynamicTableHelper {

    private static YumengMybatisPlusProperties mybatisPlusProperties;
    private static YumengMybatisPlusProperties getMybatisPlusProperties() {
        if (mybatisPlusProperties == null) {
            mybatisPlusProperties = PropHelper.getMybatisPlusProperties();
        }
        return mybatisPlusProperties;
    }
    //动态表名处理器
    private volatile static SimpleTableNameHandler simpleTableNameHandler;

    public static SimpleTableNameHandler tableNameHandler(){
        if (getMybatisPlusProperties().getPlugins().getEnableDynamicTableName()){
            if (simpleTableNameHandler == null){
                synchronized (DynamicTableHelper.class){
                    if (simpleTableNameHandler == null){
                        simpleTableNameHandler = SpringUtils.getBean(SimpleTableNameHandler.class);
                    }
                }
            }
        }
        return simpleTableNameHandler;
    }

    //region setParam 参数设置
    public static void setIdParam(String tableName, Long obj){
        setIdParam(tableName, obj, 1);
    }
    public static void setIdParam(String tableName, Long obj, int tableCount){
        DynamicTableNameMode mode = DynamicTableNameMode.ID_MOD;
        Optional.ofNullable(tableNameHandler()).ifPresent(p->{
            AbstractTableNameMapParser parser = p.getAbstractTableNameParser(mode);
            if (parser == null){
                throw new YumengException("请配置【%s】表名处理器", mode.getDesc());
            }
            if (!parser.checkContains(tableName)){
                throw new YumengException("表【%s】不在【%s】表名处理器配置内", tableName, mode.getDesc());
            }
            parser.setParam(tableName, obj, tableCount);
        });
    }

    public static void setDateParam(String tableName, String date){
        setDateParam(tableName, date, 1);
    }
    public static void setDateParam(String tableName, String date, int tableCount){
        DynamicTableNameMode mode = DynamicTableNameMode.DATE;
        Optional.ofNullable(tableNameHandler()).ifPresent(p->{
            AbstractTableNameMapParser parser = p.getAbstractTableNameParser(mode);
            if (parser == null){
                throw new YumengException("请配置【%s】表名处理器", mode.getDesc());
            }
            if (!parser.checkContains(tableName)){
                throw new YumengException("表【%s】不在【%s】表名处理器配置内", tableName, mode.getDesc());
            }
            parser.setParam(tableName, date, tableCount);
        });
    }

    public static void setDateParam(String tableName, String kssj, String jssj){
        setDateParam(tableName, kssj, jssj, 1);
    }
    public static void setDateParam(String tableName, String kssj, String jssj, int tableCount){
        DynamicTableNameMode mode = DynamicTableNameMode.DATE;
        Optional.ofNullable(tableNameHandler()).ifPresent(p->{
            AbstractTableNameMapParser parser = p.getAbstractTableNameParser(mode);
            if (parser == null){
                throw new YumengException("请配置【%s】表名处理器", mode.getDesc());
            }
            if (!parser.checkContains(tableName)){
                throw new YumengException("表【%s】不在【%s】表名处理器配置内", tableName, mode.getDesc());
            }
            ((DateTableNameParser)parser).setParam(tableName, kssj, jssj, tableCount);
        });
    }

    //endregion

    /**
     * 检查本地存储参数是否及时清理
     *
     */
    public static void checkLocalParamList(){
        if (!getMybatisPlusProperties().getPlugins().getEnableDynamicTableName()){
            return;
        }
        for (ThreadLocal<?> td : AbstractTableNameMapParser.getLocalParamList()){
            Object val = td.get();
            if (val != null){
                log.warn("动态表名插件本地存储未清理:{}", val);
                td.remove();
            }
        }
    }

}
